import {createRouter, createWebHistory} from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import UserManage from '@/components/user/UserManage.vue'
import SysConfig from '@/components/systemconfig/SysConfig.vue'
import Table from '@/components/commen/CustomCommTable.vue'
import TestComm from '@/components/commen/testComm.vue'
import Test from '@/components/test/test.vue'
import TestB from '@/components/test/testB.vue'
const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        name: 'Home',
        redirect: '/welcome',
        component: Home,
        children: [
            {path: '/welcome', component: Welcome,},
            {path: '/userManage', component: UserManage,},
            {path: '/sysConfig', component: SysConfig,},
            {path: '/table', component: Table,},
            {path: '/testComm', component: TestComm,},
            {path: '/test', component: Test,},
            {path: '/testB', component: TestB,},
        ]
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // }
]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//挂载路由守卫
router.beforeEach((to, from, next) => {
    /**
     * to 要访问的路径
     * from 代表从哪个路径过来
     * nex 一个函数 代表放心
     *  next() 放行 next('/login') 强行跳转
     */
    // console.log(to);
    //登录页面放行
    if (to.path === '/login') {
        // console.log(to);
        return next();
    }
    const token = window.sessionStorage.getItem('token')
    //没有token就跳转login页面
    if (!token) {
        // console.log(to);
        return next('/login');
    } else {
        next();
    }
})

export default router
